package fr.playsoft.psas;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import fr.playsoft.psas.maps.MyItemizedOverlay;

public class MapLocation extends MapActivity {
    
	public static final String[] SPINNER_TEXT = {
		"Current location",
		"Paris - France", 
		"London - UK", 
		"Lille - France",
		"Cannes - France"
	};
	
	public static final String[] MAP_SNIPPET_TEXT = {
		"You're standing here",
		"Paris - Location rom MobisanT project", 
		"London - where my friends go to study abroad", 
		"Lille - Location from Happy Lille project",
		"Cannes - Location from Festival de Cannes project"
	};
	
	public static final double GEO_PARIS_LAT = 48.85737;
	public static final double GEO_PARIS_LNG = 2.34923;
	
	public static final double GEO_LONDON_LAT = 51.50074;
	public static final double GEO_LONDON_LNG = -0.1244;
	
	public static final double GEO_LILLE_LAT = 50.62967;
	public static final double GEO_LILLE_LNG = 3.0574;
	
	public static final double GEO_CANNES_LAT = 43.55283;
	public static final double GEO_CANNES_LNG = 7.0174;
	
	private Spinner mSpinner;
	private MapView mMapView;
	private MapController mc;
		
	private static Location mCurrentLocation;
	private int mSelectedIndex;
	private GeoPoint[] mGeoPoint;
	
	/** Called when the activity is first created. */
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
				
		setContentView(R.layout.maplocation);
		
		mCurrentLocation = getCurrentPissition(this);
		
		mSelectedIndex = 0;
		mGeoPoint = new GeoPoint[SPINNER_TEXT.length];
		mGeoPoint[0] = new GeoPoint((int)(mCurrentLocation.getLatitude()*1E6), 
				(int) (mCurrentLocation.getLongitude()*1E6));
		mGeoPoint[1] = new GeoPoint((int) (GEO_PARIS_LAT*1E6), (int) (GEO_PARIS_LNG*1E6));
		mGeoPoint[2] = new GeoPoint((int) (GEO_LONDON_LAT*1E6), (int) (GEO_LONDON_LNG*1E6));
		mGeoPoint[3] = new GeoPoint((int) (GEO_LILLE_LAT*1E6), (int) (GEO_LILLE_LNG*1E6));
		mGeoPoint[4] = new GeoPoint((int) (GEO_CANNES_LAT*1E6), (int) (GEO_CANNES_LNG*1E6));
		
		setupMapView();
		setupSpinnerData();
		
	}

	private void setupMapView() {
		
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapView.setBuiltInZoomControls(true);
		
		List<Overlay> mapOverlays = mMapView.getOverlays();
		
		Drawable drawable = getResources().getDrawable(R.drawable.marker);
		MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(drawable, mMapView);
		
		for(int i = 0; i < SPINNER_TEXT.length; i++) {
			OverlayItem overlayItem = new OverlayItem(mGeoPoint[i], SPINNER_TEXT[i], MAP_SNIPPET_TEXT[i]);
			itemizedOverlay.addOverlay(overlayItem);
		}
		
		mapOverlays.add(itemizedOverlay);
        
		mc = mMapView.getController();
		mc.animateTo(mGeoPoint[mSelectedIndex]);
		mc.setZoom(16);
				
	}

	private void setupSpinnerData() {
		mSpinner = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, SPINNER_TEXT);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		mSpinner.setAdapter(adapter);
		
		mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
		    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, 
		    		int position, long id) {
		    	mSelectedIndex = position;
		    	mc.animateTo(mGeoPoint[mSelectedIndex]);
		    }

		    public void onNothingSelected(AdapterView<?> parentView) {
		        // your code here
		    }

		});
		
	}
		
	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}
	
	private static final LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
        	mCurrentLocation = location; 
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    
    public Location getCurrentPissition(Context context){
    	LocationManager locationManager;
    	Criteria criteria = new Criteria();
    	String bestProvider = "";
    	
    	
		locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		locationManager.getAllProviders();
		bestProvider = locationManager.getBestProvider(criteria, false);
		locationManager.requestLocationUpdates(bestProvider, 1000L, 500.0f, locationListener);
		mCurrentLocation = locationManager.getLastKnownLocation(bestProvider);
		if (mCurrentLocation == null)
		{
			// sometimes, the location was not initialed will cause problems, dump_provider is nothing
			mCurrentLocation = new Location("dump_provider");
		}
		
		// Always beware that listening for a long time consumes a lot of battery power, 
		// so as soon as you have the information you need, you should stop listening
		locationManager.removeUpdates(locationListener);
    							
    	return mCurrentLocation;
	}
	
}